package com.cloudkinto.appletservice.stock.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloudkinto.appletservice.stock.AppletStockLogService;
import com.cloudkinto.appletservice.stock.vo.log.AppletStockLogPageRes;
import com.cloudkinto.common.web.PageResult;
import com.cloudkinto.common.web.SingleResult;
import com.cloudkinto.dao.ShopDao;
import com.cloudkinto.dao.StockLogDao;
import com.cloudkinto.dao.StorageDao;
import com.cloudkinto.dao.UserDao;
import com.cloudkinto.entity.ShopDo;
import com.cloudkinto.entity.StockLogDo;
import com.cloudkinto.entity.StorageDo;
import com.cloudkinto.service.finance.vo.IdAndNameEntity;
import com.cloudkinto.service.shop.ShopService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * service实现类
 * </p>
 *
 * @author 张永远
 * @since 2020-06-10
 */
@Service
@Transactional(rollbackFor = Exception.class)//事务回滚
public class AppletStockLogServiceImpl extends ServiceImpl<StockLogDao, StockLogDo> implements AppletStockLogService {
    @Autowired
    private StockLogDao dao;
    @Autowired
    private ShopDao shopDao;
    @Autowired
    private StorageDao storageDao;
    @Autowired
    private ShopService shopService;



    @Override
    public PageResult getListPage(Map<String, String> map) {
        QueryWrapper<StockLogDo> wrapper = queryBuild(map);
        IPage<StockLogDo> page = pageInit(map);
        page = this.page(page, wrapper);
        List<AppletStockLogPageRes> resList = handlerListAfter(page.getRecords());
        PageResult result = new PageResult(resList, (int) page.getCurrent(), (int) page.getSize(), (int) page.getTotal(), (int) page.getPages());
        return result;
    }

    //查询分页列表之后对列表数据的处理
    private List<AppletStockLogPageRes> handlerListAfter(List<StockLogDo> list) {
        List<AppletStockLogPageRes> result = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            for (StockLogDo stockLogDo : list) {
                AppletStockLogPageRes res = new AppletStockLogPageRes();
                res.setId(stockLogDo.getId());
                res.setAfterCount(stockLogDo.getAfterCount());
                res.setCommoditySku(stockLogDo.getCommoditySku());
                res.setCommoditySku(stockLogDo.getCommoditySku());
                res.setCount(stockLogDo.getCount());
                res.setCreateTime(stockLogDo.getCreateTime());
                ShopDo shopDo = shopDao.selectById(stockLogDo.getShopId());
                if (shopDo != null) {
                    res.setShop(shopDo.getShopName());
                }
                StorageDo storageDo = storageDao.selectById(stockLogDo.getStorageId());
                if (storageDo != null) {
                    res.setStorage(storageDo.getStorageName());
                }
                res.setThirdOrderNumber(stockLogDo.getThirdOrderNumber());
                if (stockLogDo.getType() == 0) {
                    res.setType("仓库盘点");
                } else if (stockLogDo.getType() == 1) {
                    res.setType("出库");
                } else if (stockLogDo.getType() == 2) {
                    res.setType("入库");
                }
                result.add(res);
            }
        }
        return result;
    }

    private IPage<StockLogDo> pageInit(Map<String, String> map) {
        IPage<StockLogDo> page = new Page();
        page.setSize(StringUtils.isNotBlank(map.get("pageSize")) ? Long.parseLong(map.get("pageSize")) : 10);
        page.setCurrent(StringUtils.isNotBlank(map.get("current")) ? Long.parseLong(map.get("current")) : 1);
        return page;
    }

    private QueryWrapper<StockLogDo> queryBuild(Map<String, String> map) {
        QueryWrapper<StockLogDo> wrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(map.get("keyword"))) {
            wrapper.and(i -> {
                i.like("commodity_sku", map.get("keyword"))
                        .or().like("third_order_number", map.get("keyword"));
            });
        }
        if (StringUtils.isNotBlank(map.get("shopId"))) {
            wrapper.eq("shop_id", map.get("shopId"));
        }
        if (StringUtils.isNotBlank(map.get("endDate"))) {
            wrapper.le("create_time", map.get("endDate"));
        }
        if (StringUtils.isNotBlank(map.get("startDate"))) {
            wrapper.ge("create_time", map.get("startDate"));
        }
        if (StringUtils.isNotBlank(map.get("storageId"))) {
            wrapper.eq("storage_id", map.get("storageId"));
        }
        wrapper.eq("company_id", map.get("companyId")).orderByDesc("id");
        return wrapper;
    }


}
